/**
 * @author YinHang
 */
;(function () {
	var g2d = Gamepackage.get("Gamepackage.ui.g2d");
	g2d.Layer = function (width, height) {
		this.visibles = [];
		this.canvas = new Gamepackage.ui.g2d.Canvas(document.createElement("canvas"), 1, 1);
		this.setWidth(width);
		this.setHeight(height);
	};
	
	Gamepackage.extend(g2d.Layer, g2d.Displayable);
	var Prototype = g2d.Layer.prototype;
	
	Prototype.add = function (visible) {
		this.visibles.push(visible);
	};
	
	Prototype.setWidthSuper = Prototype.setWidth;
	Prototype.setHeightSuper = Prototype.setHeight;
	
	Prototype.setWidth = function (width) {
		this.setWidthSuper(width);
		this.canvas.setWidth(width);
	};
	
	Prototype.setHeight = function (height) {
		this.setHeightSuper(height);
		this.canvas.setHeight(height);
	};
	
	Prototype.remove = function (index) {
		visible[index] = null;
	};
	
	Prototype.getGraphics = function () {
		return this.canvas.graphics;
	};
	
	Prototype.clear = function (color) {
		this.canvas.getGraphics().fillRect(color, 0, 0, this.width, this.height);
	};
	
	Prototype.drawToBuffer = function () {
		var g = this.canvas.getGraphics();
		var visibleLength = this.visibles.length;
		for(var i=0;i<visibleLength;++i)
			if(this.visibles[i] && this.visibles[i].getVisible())
				this.visibles[i].draw(g);
	};
	
	Prototype.draw = function (graphics) {
		this.clear("rgb(255, 255, 255)");
		this.drawToBuffer();
		graphics.drawCanvas(this.canvas, this.x, this.y, this.width, this.height);
	};
})();
